import datetime
from datetime import datetime
from datetime import timedelta

from .trade_date import trade_date_sse


def get_real_trade_date(date, towards):
    """ 获取真实的交易日期,其中,第三个参数towards是表示向前/向后推

    :param date: 日期
    :param towards:  1 日期向后迭代, -1 日期向前迭代
    :return:
    """
    delta = timedelta(days=1)
    delta = -delta if towards == 1 else delta

    while date not in trade_date_sse:
        date = str(datetime.strptime(date, '%Y-%m-%d') - delta)[0:10]

    return date
